﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SiteManager.Core.Helper
{
    public class CacheHelper
    {
        private static CacheHelper cache = new CacheHelper();

        private CacheHelper() { }

        public static CacheHelper Get { get { return cache; } }

        public object this[string key]
        {
            get
            {
                return HttpRuntime.Cache[key];
            }
        }

        public static bool Insert(string key, object obj)
        {
            return Insert(key, ConfigHelper.CacheTime, obj);
        }

        public static bool Insert(string key, int seconds, object obj)
        {
            try
            {
                if (HttpRuntime.Cache[key] != null)
                    return true;

                HttpRuntime.Cache.Insert(
                    key,
                    obj,
                    null,
                    DateTime.Now.AddSeconds(seconds),
                    TimeSpan.Zero
                );
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool Remove(string key)
        {
            try
            {
                HttpRuntime.Cache.Remove(key);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}